package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.StringPool;
import cn.cnf.common.utils.UserRequestUtil;
import cn.cnf.entity.User;
import cn.cnf.entity.vo.FieldUploadResult;
import cn.cnf.service.FileService;
import cn.cnf.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author ZhuMeiYi
 * @date 2020/12/5 13:04
 * @description
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;

    /**
     * 上传某章图片
     *
     * @param files     图片
     * @param comicId   漫画id
     * @param chapterId 章节id
     * @return
     */
    @RequestMapping("/uploadComicChapterImg")
    public Result uploadComicChapterImg(@RequestParam("file") MultipartFile[] files,
                                        @RequestParam("comicId") String comicId,
                                        @RequestParam("chapterId") String chapterId) {
        User user = userService.findUserByUsername(UserRequestUtil.getCurrentUserName());

        // 图片相对路径
        final String folder = StringPool.comicPath + "/" + comicId + "/" + chapterId;
        List<FieldUploadResult> results = fileService.multipleImageUpload(files, folder, user.getNickname());
        return Result.success(results);
    }

    /**
     * 上传图片无水印
     *
     * @param files
     * @return
     */
    @RequestMapping("/uploadImg")
    public Result uploadImg(@RequestParam("file") MultipartFile[] files) {
        User user = userService.findUserByUsername(UserRequestUtil.getCurrentUserName());
        // 图片相对路径
        List<FieldUploadResult> results = fileService.multipleImageUpload(files, StringPool.IMG_PATH+"/"+user.getUsername(), null);
        return Result.success(results);
    }

    /**
     * 上传图片有水印
     *
     * @param files
     * @return
     */
    @RequestMapping("/uploadImgWithMark")
    public Result uploadImgWithMark(@RequestParam("file") MultipartFile[] files) {
        User user = userService.findUserByUsername(UserRequestUtil.getCurrentUserName());
        // 图片相对路径
        List<FieldUploadResult> results = fileService.multipleImageUpload(files, StringPool.IMG_PATH+"/"+user.getUsername(), user.getUsername());
        return Result.success(results);
    }

    /**
     * 上传用户头像
     *
     * @param files
     * @return
     */
    @RequestMapping("/uploadUserIcon")
    public Result uploadUserIcon(@RequestParam("file") MultipartFile[] files) {
        // 图片相对路径
        List<FieldUploadResult> results = fileService.multipleImageUpload(files, StringPool.userIconPath, "");
        return Result.success(results);
    }
}
